package org.cnc.app;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.KeyEventPostProcessor;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JSplitPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import org.cnc.core.CncCore;
import org.cnc.gui.ModelDrawer;
import org.cnc.gui.SettingsPanel;
import org.cnc.gui.StatusPanel;
import org.jvnet.substance.skin.SubstanceRavenGraphiteGlassLookAndFeel;

public class App implements KeyEventPostProcessor {

	private JFrame frame;
	private ModelDrawer modelDrawer;
	private SettingsPanel settingsPanel;
	private StatusPanel statusPanel;
	private CncCore core;
	private JButton button;
	private boolean machineStarted = false;
	
	public App() {
		
		frame = new JFrame("CncEmulator");
		
		core = new CncCore();
		
		modelDrawer = new ModelDrawer();
	//	core.connectModelDrawer( modelDrawer );
		
		settingsPanel = new SettingsPanel( );
		settingsPanel.addChangeListener(core);		
		
		statusPanel = new StatusPanel();
		core.connectStatusPanel( statusPanel );
		
	    frame.setSize(1046, 755);
	    frame.setLocation(300,200);
	    frame.getContentPane().setLayout(new BorderLayout(0, 0));
	    
	    JSplitPane splitPane = new JSplitPane();
	    splitPane.setOneTouchExpandable(true);
	    splitPane.setContinuousLayout(true);
	    splitPane.setBackground(Color.DARK_GRAY);
	    frame.getContentPane().add(splitPane, BorderLayout.CENTER);
	    
	    JSplitPane splitPane_1 = new JSplitPane();
	    splitPane_1.setContinuousLayout(true);
	    splitPane_1.setOneTouchExpandable(true);
	    splitPane_1.setBackground(Color.DARK_GRAY);
	    splitPane_1.setForeground(Color.LIGHT_GRAY);
	    splitPane_1.setOrientation(JSplitPane.VERTICAL_SPLIT);
	    splitPane.setLeftComponent(splitPane_1);
	    
	    splitPane_1.setLeftComponent(settingsPanel);
	    splitPane_1.setRightComponent(statusPanel);
	    
	    splitPane.setRightComponent( modelDrawer );
	    
	    button = new JButton("\u0421\u0442\u0430\u0440\u0442");
	    button.addActionListener(new ActionListener() {
	    	public void actionPerformed(ActionEvent arg0) {
	    		
	    		if ( machineStarted )
	    		{
	    			((JButton)arg0.getSource()).setText( "Старт" );
	    			core.stopMachine();
	    			machineStarted = false;
	    		}
	    		else
	    		{
	    			((JButton)arg0.getSource()).setText( "Стоп" );
	    			core.startMachine();
	    			machineStarted = true;
	    		}
	    	}
	    });
	    
	    modelDrawer.add(button, BorderLayout.NORTH);
	    //modelDrawer.add(stopBtn, BorderLayout.NORTH);
	    
	    frame.setVisible(true);
	    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
	}
	
	/**
	 * @param args
	*/
	public static void main(String[] args) {
        
		SwingUtilities.invokeLater ( new Runnable()
		{
			public void run ()
			{
				try
				{
				    UIManager.setLookAndFeel (new SubstanceRavenGraphiteGlassLookAndFeel()  );
					
				    App application = new App();
				    
				    KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(application);
				}
				catch ( Exception e )
				{
				    System.out.println ( e );
				}
			}
		} );
		
	}

	@Override
	public boolean postProcessKeyEvent( final KeyEvent e) {
		
        if (e.getID() == KeyEvent.KEY_PRESSED) { 
        	
        	core.pressKey( e.getKeyCode() );
        } 
        
		return false;
	}

}
